#!/bin/bash

#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Copyright (c) 2024 Robin Jarry
#
TS_TOPDIR="${0%/*}/../.."
TS_DESC="bits"

. "$TS_TOPDIR"/functions.sh
ts_init "$*"

ts_check_test_command "$TS_CMD_BITS"
ts_cd "$TS_OUTDIR"

ts_init_subtest "default"
$TS_CMD_BITS 11,22,33,44 >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "mask"
$TS_CMD_BITS --mask 11,22,33,44 >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "grouped-mask"
$TS_CMD_BITS --grouped-mask 11,22,33,44 >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "list"
$TS_CMD_BITS --list 11,22,33,44 >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "binary"
$TS_CMD_BITS --binary 11,22,33,44 >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "truncate"
$TS_CMD_BITS -l 1,10000 >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "width"
$TS_CMD_BITS -w 16384 -l 10000  >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "width-truncate"
$TS_CMD_BITS -w 32 -l 11,22,33,44 >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "parse-mask"
$TS_CMD_BITS -l 0x0badcaca >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "parse-range"
$TS_CMD_BITS -g 50-100 75-150 >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "parse-grouped-mask"
$TS_CMD_BITS -l ,9000000,00000000,0c000000,00000000 >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "or"
$TS_CMD_BITS -l 50-100 '|75-150' >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "and"
$TS_CMD_BITS -l 50-100 '&75-150' >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "xor"
$TS_CMD_BITS -l 50-100 '^75-150' >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "not"
$TS_CMD_BITS -l 50-100 '~75-150' >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_init_subtest "stdin"
{
	echo 11,22,33,44
	echo ^22
} | $TS_CMD_BITS --list >> $TS_OUTPUT 2>> $TS_ERRLOG
ts_finalize_subtest

ts_finalize
